

using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;

public class SceneController :MonoBehaviour
{
    public int state = 0;//0休闲 1加载中

    public void Load(string next)
    {
        LoadingViewController.Instance.Open();
        LoginViewController.Instance.Close();
        state = 1;
        StartCoroutine(LoadSceneAsync(next));

    }

    private IEnumerator LoadSceneAsync(string next)
    {
        var op = SceneManager.LoadSceneAsync(next);
        op.allowSceneActivation = false ;
        while(op.progress < 0.9f)
        {
            yield return new WaitForEndOfFrame();
            LoadingViewController.Instance.UpdateLoadingProcess(op.progress);
        }
        //0.9 - 1
        float progress = op.progress;
        while (progress <= 1f)  //卡住进度条
        {
            progress += GameTime.deltaTime;
            LoadingViewController.Instance.UpdateLoadingProcess(progress);
            yield return new WaitForEndOfFrame();
        }
        op.allowSceneActivation = true;

        yield return op;
        var player  =  UnitManager.Instance.CreatePlayer();
        GameSystem.Instance.uCameraController.SetTarget(player.transform);

        yield return new WaitForEndOfFrame();
        LoadingViewController.Instance.Close();
        state = 0;
        MainViewController.Instance.Open();
        //yield return op;
    }
}